package com.zbcz.erp.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.zbcz.erp.bean.DoneJobBean
import com.zbcz.erp.databinding.ItemCommonListItemBinding
import com.zbcz.erp.fragment.CommonListFragment.Companion.MY_APPLY
import com.zbcz.erp.fragment.CommonListFragment.Companion.MY_DONE_LIST
import com.zbcz.erp.fragment.CommonListFragment.Companion.MY_TO_DO_LIST
import com.zbcz.erp.util.onSingleClick

class CommonListAdapter(private val type: Int) :
    ListAdapter<DoneJobBean, CommonListAdapter.ItemViewHolder>(DiffCallback()) {

    var onItemClickListener: ((Int, DoneJobBean) -> Unit)? = null

    /**
     * 将毫秒值转换为天、小时、分钟、秒的格式
     *
     * @param millis 毫秒值
     * @return 格式化后的字符串，如 "1天10小时10分10秒"
     */
    fun formatDuration(millis: Long): String {
        val seconds = millis / 1000
        val minutes = seconds / 60
        val hours = minutes / 60
        val days = hours / 24

        val displaySeconds = seconds % 60
        val displayMinutes = minutes % 60
        val displayHours = hours % 24

        val dayPart = if (days > 0) "${days}天" else ""
        val hourPart = if (displayHours > 0) "${displayHours}小时" else ""
        val minutePart = if (displayMinutes > 0) "${displayMinutes}分" else ""
        val secondPart = if (displaySeconds > 0) "${displaySeconds}秒" else ""

        return "$dayPart$hourPart$minutePart$secondPart".ifEmpty { "0秒" }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val binding =
            ItemCommonListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ItemViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val item = getItem(position)
        holder.bind(item)
    }

    inner class ItemViewHolder(private val binding: ItemCommonListItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        init {
            binding.root.onSingleClick {
                onItemClickListener?.invoke(adapterPosition, currentList[adapterPosition])
            }
        }

        fun bind(item: DoneJobBean) {
            when (type) {
                MY_TO_DO_LIST -> {
                    binding.tvApprovalAdvice.visibility = View.GONE
                    binding.tvApprovalAdviceValue.visibility = View.GONE
                    binding.tvTimeConsumingValue.visibility = View.GONE
                    binding.tvTimeConsuming.visibility = View.GONE
                }

                MY_DONE_LIST -> {
                    binding.tvStatus.text = "审批操作："
                }

                MY_APPLY -> {
                    binding.tvApprovalAdvice.visibility = View.GONE
                    binding.tvApprovalAdviceValue.visibility = View.GONE
                }
            }

            binding.tvTime.text = "创建时间：${item.createTime}"
            binding.tvBusiness.text = item.name
            binding.tvProcessValue.text = "所属流程：${item.processName}"
            binding.tvDelegatePersonValue.text = item.assignee ?: "无"
            binding.tvProcessStartPersonValue.text = item.applyer
            //binding.tvProcessPriorityValue.text = item.priority.toString()
            binding.tvApprovalAdviceValue.text = item.deleteReason ?: "无"
            binding.tvStatusValue.text = if (item.isSuspended) "挂起" else "激活"
            //： 0 ：普通  ，1 ：重要 ，2 紧急
            val mPriorityText = when (item.priority) {
                1 -> "重要"
                2 -> "紧急"
                else -> "普通"
            }
            binding.tvProcessPriorityValue.text = mPriorityText
            binding.tvTimeConsumingValue.text = formatDuration(item.duration)
            binding.tvBusiness.text = item.name
        }
    }

    class DiffCallback : DiffUtil.ItemCallback<DoneJobBean>() {
        override fun areItemsTheSame(oldItem: DoneJobBean, newItem: DoneJobBean): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: DoneJobBean, newItem: DoneJobBean): Boolean {
            return oldItem == newItem
        }
    }
}
